//
//  ShowImageController.m
//  Conference
//
//  Created by 陈鑫 on 12-12-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ShowImageController.h"
#import "Globals.h"
@interface ShowImageController ()
@end

@implementation ShowImageController
@synthesize url;
@synthesize scrollView;
@synthesize imageView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.imageView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"attachment_default_pic.jpg"]]autorelease];
    self.imageView.contentMode = UIViewContentModeCenter;
    self.imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
    
    self.imageView.center=CGPointMake(self.scrollView.center.x, self.view.center.y);
    
    [self.scrollView addSubview:self.imageView];
}

-(void)viewWillAppear:(BOOL)animated{
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];

}

-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *Url=[NSString stringWithFormat:@"%@%@",[Globals getInstance].servierUrl,self.url];
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[Url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    UIImage *image=[UIImage imageWithData:data];
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:image waitUntilDone:YES];
    
	[pool release];
    
}
-(void)onPostExecute:(UIImage*)img{
    if(img!=nil)
    {
        NSArray *views=self.imageView.subviews;
        for(UIView *view in views){
            [view removeFromSuperview];}
    
        [self.imageView setImage:img];
        
        self.imageView.frame=CGRectMake(0, 0, img.size.width, img.size.height);
        
        self.imageView.center=CGPointMake(self.scrollView.center.x,70+self.imageView.frame.size.height/2);
        
        [self.scrollView addSubview:self.imageView];
        [self.scrollView setContentSize:CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height)];
    }
    
    
}
- (void)viewDidUnload
{
    [self setImageView:nil];
    [self setUrl:nil];
    [self setScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [imageView release];
    [url release];
    [scrollView release];
    [super dealloc];
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
